// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class EmployeeMapper
    {

        /// <summary />
        static EmployeeMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Employee> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.EmployeeSelectAll);

            return Load(commands, dataMappers);
        }

        private Employee.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Employee.DataTransfer data=new Employee.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Employee.DataTransfer FillData(Employee.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("EmployeeID");
            if (!record.IsDBNull(ord))
                data.EmployeeID=record.GetInt32(ord);

            ord=record.GetOrdinal("LastName");
            if (!record.IsDBNull(ord))
                data.LastName=record.GetString(ord);

            ord=record.GetOrdinal("FirstName");
            if (!record.IsDBNull(ord))
                data.FirstName=record.GetString(ord);

            ord=record.GetOrdinal("Title");
            if (!record.IsDBNull(ord))
                data.Title=record.GetString(ord);

            ord=record.GetOrdinal("TitleOfCourtesy");
            if (!record.IsDBNull(ord))
                data.TitleOfCourtesy=record.GetString(ord);

            ord=record.GetOrdinal("BirthDate");
            if (!record.IsDBNull(ord))
                data.BirthDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("HireDate");
            if (!record.IsDBNull(ord))
                data.HireDate=record.GetDateTime(ord);

            ord=record.GetOrdinal("Address");
            if (!record.IsDBNull(ord))
                data.Address=record.GetString(ord);

            ord=record.GetOrdinal("City");
            if (!record.IsDBNull(ord))
                data.City=record.GetString(ord);

            ord=record.GetOrdinal("Region");
            if (!record.IsDBNull(ord))
                data.Region=record.GetString(ord);

            ord=record.GetOrdinal("PostalCode");
            if (!record.IsDBNull(ord))
                data.PostalCode=record.GetString(ord);

            ord=record.GetOrdinal("Country");
            if (!record.IsDBNull(ord))
                data.Country=record.GetString(ord);

            ord=record.GetOrdinal("HomePhone");
            if (!record.IsDBNull(ord))
                data.HomePhone=record.GetString(ord);

            ord=record.GetOrdinal("Extension");
            if (!record.IsDBNull(ord))
                data.Extension=record.GetString(ord);

            ord=record.GetOrdinal("Notes");
            if (!record.IsDBNull(ord))
                data.Notes=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Employee.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.EmployeeSelect,
                    parameterBuilder.GetMarker("EmployeeID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("EmployeeID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.EmployeeDelete,
                    parameterBuilder.GetMarker("EmployeeID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("EmployeeID", 1),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.EmployeeID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.EmployeeInsert,
                    parameterBuilder.GetMarker("LastName", 1),
                    parameterBuilder.GetMarker("FirstName", 2),
                    parameterBuilder.GetMarker("Title", 3),
                    parameterBuilder.GetMarker("TitleOfCourtesy", 4),
                    parameterBuilder.GetMarker("BirthDate", 5),
                    parameterBuilder.GetMarker("HireDate", 6),
                    parameterBuilder.GetMarker("Address", 7),
                    parameterBuilder.GetMarker("City", 8),
                    parameterBuilder.GetMarker("Region", 9),
                    parameterBuilder.GetMarker("PostalCode", 10),
                    parameterBuilder.GetMarker("Country", 11),
                    parameterBuilder.GetMarker("HomePhone", 12),
                    parameterBuilder.GetMarker("Extension", 13),
                    parameterBuilder.GetMarker("Notes", 14)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("LastName", 1),
                DbType.String,
                20,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.LastName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("FirstName", 2),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.FirstName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Title", 3),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Title ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("TitleOfCourtesy", 4),
                DbType.String,
                25,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.TitleOfCourtesy ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("BirthDate", 5),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.BirthDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("HireDate", 6),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.HireDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Address", 7),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Address ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("City", 8),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.City ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Region", 9),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Region ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("PostalCode", 10),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.PostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Country", 11),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Country ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("HomePhone", 12),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.HomePhone ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Extension", 13),
                DbType.String,
                4,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Extension ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Notes", 14),
                DbType.String,
                8192,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Notes ?? (object)DBNull.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Employee.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.EmployeeUpdate,
                    parameterBuilder.GetMarker("LastName", 1),
                    parameterBuilder.GetMarker("FirstName", 2),
                    parameterBuilder.GetMarker("Title", 3),
                    parameterBuilder.GetMarker("TitleOfCourtesy", 4),
                    parameterBuilder.GetMarker("BirthDate", 5),
                    parameterBuilder.GetMarker("HireDate", 6),
                    parameterBuilder.GetMarker("Address", 7),
                    parameterBuilder.GetMarker("City", 8),
                    parameterBuilder.GetMarker("Region", 9),
                    parameterBuilder.GetMarker("PostalCode", 10),
                    parameterBuilder.GetMarker("Country", 11),
                    parameterBuilder.GetMarker("HomePhone", 12),
                    parameterBuilder.GetMarker("Extension", 13),
                    parameterBuilder.GetMarker("Notes", 14),
                    parameterBuilder.GetMarker("EmployeeID", 15)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("LastName", 1),
                DbType.String,
                20,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.LastName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("FirstName", 2),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.FirstName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Title", 3),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Title ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("TitleOfCourtesy", 4),
                DbType.String,
                25,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.TitleOfCourtesy ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("BirthDate", 5),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.BirthDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("HireDate", 6),
                DbType.Date,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.HireDate
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Address", 7),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Address ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("City", 8),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.City ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Region", 9),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Region ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("PostalCode", 10),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.PostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Country", 11),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Country ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("HomePhone", 12),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.HomePhone ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Extension", 13),
                DbType.String,
                4,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Extension ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Notes", 14),
                DbType.String,
                8192,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Notes ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("EmployeeID", 15),
                DbType.Int32,
                ParameterDirection.Input,
                string.Empty,
                DataRowVersion.Default,
                data.EmployeeID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
